package com.openweixin.sheknows.Listener;

import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;

import com.openweixin.sheknows.controller.ExpertController;
import com.openweixin.sheknows.service.QuestionService;
import com.openweixin.sheknows.util.SessionUtil;
import com.openweixin.sheknows.util.SpringContextUtils;

public class SessionAttributeListener implements HttpSessionAttributeListener{

	@Override
	public void attributeAdded(HttpSessionBindingEvent arg0) {
		
	}

	@Override
	public void attributeRemoved(HttpSessionBindingEvent event) {
		unLockQuestion(event);;
	}

	@Override
	public void attributeReplaced(HttpSessionBindingEvent event) {
		unLockQuestion(event);;
	}
	
	private void unLockQuestion(HttpSessionBindingEvent event){
		
		try {
			if(ExpertController.LOCK_QUESTION_KEY.equals(event.getName()) ){
				Integer qid=(Integer)event.getValue();
				Integer uid=(Integer)event.getSession().getAttribute(SessionUtil.USER_ID_KEY);
				QuestionService service=(QuestionService)SpringContextUtils.getContext().getBean("questionServiceImpl");
				service.unlockQuestionByExtpert(qid, uid);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	

}
